package com.cxg.fairy.ec.sign;

import android.app.Activity;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.design.widget.TextInputEditText;
import android.util.Log;
import android.view.View;

import com.cxg.fairy.delegates.FairyDelegate;
import com.cxg.fairy.ec.R;
import com.cxg.fairy.ec.R2;
import com.cxg.fairy.net.RestClient;
import com.cxg.fairy.net.callback.ISuccess;
import com.cxg.fairy.util.log.FairyLogger;


import butterknife.BindView;
import butterknife.OnClick;

/**
 * Created by Administrator on 2017/11/6.
 */

public class SignUpDelegate extends FairyDelegate {

    @BindView(R2.id.edit_reg_account)
    TextInputEditText tAccount = null;

    @BindView(R2.id.edit_reg_code)
    TextInputEditText tRegCode = null;

    private ISignListener mISignListener = null;

    @Override
    public void onAttach(Activity activity) {
        super.onAttach(activity);
        if (activity instanceof ISignListener) {
            mISignListener = (ISignListener) activity;
        }
    }

    @OnClick(R2.id.btn_reg)
    void onClickReg() {
        if (checkFrom()) {
            RestClient.builder()
                    .url("http://api.gaoqingyy.com/index.php/api/example/users")
                    .success(new ISuccess() {
                        @Override
                        public void onSuccess(String response) {
                            Log.e("HAHHAHAAHHAHAHA", response);
                            FairyLogger.e("ResponseInfo:",response);
                            SignHandler.onSignUp(response, mISignListener);
                        }
                     })
                    .build()
                    .get();
        }
    }


    @OnClick(R2.id.link_login_tag)
    void onClickLink() {
        start(new SignInDelegate());
    }

    private boolean checkFrom() {
        final String account = tAccount.getText().toString();
        final String regCode = tRegCode.getText().toString();

        boolean isPass = true;

        if (account.isEmpty()) {
            tAccount.setError("请输入帐号");
            isPass = false;
        } else {
            tAccount.setError(null);
        }

        if (regCode.isEmpty()) {
            tRegCode.setError("请输入验证码");
            isPass = false;
        } else {
            tRegCode.setError(null);
        }
        return isPass;
    }

    @Override
    public Object setLayout() {
        return R.layout.delegate_sign_up;
    }

    @Override
    public void onBindView(@Nullable Bundle savedInstanceState, View rootView) {

    }
}
